<template>
	<div></div>
	<div>
		<vxe-toolbar size="mini" :buttons="toolbarButtons" @button-click="buttonClickEvent" ref="toolbarRef"
			custom></vxe-toolbar>
		<vxe-table ref="tableRef" size="mini" height="1000" max-height="800" align="center" border stripe
			:row-config="{ isCurrent: true, isHover: true }" :data="tableData" :edit-config="editConfig" keep-source>
			<vxe-column type="seq" width="70"></vxe-column>
			<vxe-column type="checkbox" width="50" align="center"></vxe-column>
			<vxe-column field="inventory_code" title="存货编码"></vxe-column>
			<vxe-column field="inventory_name" title="存货名称" show-overflow></vxe-column>
			<vxe-column field="specification" title="规格型号" show-overflow></vxe-column>
			<vxe-column field="price_incl_tax" title="价格含税"></vxe-column>
			<vxe-column field="product_name" title="产品名称" show-overflow></vxe-column>
			<vxe-column field="order_number" title="订货号" :edit-render="{ name: 'input' }"></vxe-column>
			<vxe-column field="brand" title="品牌" show-overflow></vxe-column>
			<vxe-column field="model" title="型号" show-overflow></vxe-column>
			<vxe-column field="manufacturer_order_number" title="厂家订货号"></vxe-column>
			<vxe-column field="quantity" title="数量" :edit-render="{ name: 'input' }"></vxe-column>
			<vxe-column field="unit" title="单位" show-overflow></vxe-column>
			<vxe-column field="easybuy_price" title="易买含税单价"></vxe-column>
			<vxe-column field="delivery_period" title="交期"></vxe-column>
			<vxe-column field="cs_remark" title="CS备注" show-overflow></vxe-column>
			<vxe-column field="pm_remark" title="PM备注" show-overflow></vxe-column>
			<vxe-column field="purchase_link" title="购买链接" show-overflow></vxe-column>
			<vxe-column field="action" title="操作" width="140">
				<template #default="{ row }">
					<vxe-button size="mini" status="error" @click="removeRow(row)">删除</vxe-button>
				</template>
			</vxe-column>
		</vxe-table>
	</div>
</template>

<script setup>
import { VxeUI } from "vxe-table";
import { ref, onMounted } from "vue";
const tableData = ref([{}]);
const tableRef = ref(null);
const toolbarRef = ref(null);
const editConfig = {
	trigger: 'click',
	mode: 'cell'
}
const toolbarButtons = [
	{
		name: "刷新",
		code: "刷新",
		status: "primary",
	},
	{
		name: "余额查询",
		code: "余额查询",
		status: "primary",
	},
	{
		name: "创建报价单",
		code: "创建报价单",
		status: "primary",
	},
	{
		name: "查看报价单",
		code: "查看报价单",
		status: "primary",
	},
	{
		name: "存货上传",
		code: "存货上传",
		status: "primary",
	},
	{
		name: "下采购单",
		code: "下采购单",
		status: "primary",
	},
	{
		name: "保存",
		code: "保存",
		status: "success",
	}, {
		name: "新增",
		code: "新增",
		status: "success",
	}
];
onMounted(() => {
	getList();
	//console.log(getBalance());
});
const getList = () => {
	Meteor.call("getYimInventory", (err, res) => {
		if (err) {
			console.error("调用错误:", err.message);
		} else {
			tableData.value = res.data;
		}
	});
};
//删除
const toDel = (row) => {
	console.log(row.id);
	VxeUI.modal
		.confirm({
			title: "提示",
			content: "确定要删除吗？",
		})
		.then((type) => {
			if (type === "confirm") {
				Meteor.call("delYimInventory", row.id, (err, res) => {
					if (err) {
						console.error("调用错误:", err.message);
					}
					else {
						VxeUI.modal.alert(`执行完成！`);
						getList();
					}
				})
			} else if (type === "cancel") {
				console.log("用户点击了取消");
				// 在这里处理取消操作
			}
		})
		.catch((err) => {
			console.error("弹窗异常：", err);
		});

}

//存货上传
const buttonClickEvent = ({ code }) => {
	if (code === "刷新") {
		location.reload();
		return;
	}
	if (code === "存货上传") {
		const $table = tableRef.value;
		const selectRecords = $table.getCheckboxRecords();
		if (selectRecords.length === 0) {
			VxeUI.modal.alert(`没有选择任何数据！`);
			return;
		}
		Meteor.call("createInventory", selectRecords, (err, res) => {
			if (err) {
				console.error("调用错误:", err.message);
			} else {
				console.log(res.data);
				VxeUI.modal.alert(`执行完成！`);
			}
		});
	}
	if (code === "下采购单") {
		Meteor.call("createPurchaseOrder", (err, res) => {
			if (err) {
				console.error("调用错误:", err.message);
			} else {
				console.log(res.data);
				VxeUI.modal.alert(`执行完成！`);
			}
		});
	}
	if (code === "创建报价单") {
		const $table = tableRef.value;
		const selectRecords = $table.getCheckboxRecords();
		if (selectRecords.length === 0) {
			VxeUI.modal.alert(`没有选择任何数据！`);
			return;
		}
		VxeUI.modal
			.confirm({
				title: "提示",
				content: `共${selectRecords.length}条数据，确定【${code}】吗？`,
			})
			.then((type) => {
				if (type === "confirm") {
					Meteor.call("createQuotation",selectRecords, (err, res) => {
						if (err) {
							console.error("调用错误:", err.message);
						} else {
							//console.log(res.data);
							//VxeUI.modal.alert(`执行完成！`);
							if (res.success === true) {
								VxeUI.modal.message({ content: res.message, status:'success' })	
							}else{
								VxeUI.modal.alert(res.message);	
							}
						}
					});
				} else if (type === "cancel") {
					console.log("用户点击了取消");
				}
			})
			.catch((err) => {
				console.error("弹窗异常：", err);
			});

	}
	if (code === "查看报价单") {
		location.href = "/quotation-list";
	}
	if (code === "新增") {
		pushEvent();
		return;
	}
	if (code === "保存") {
		const $table = tableRef.value;
		const insertRecords = $table.getInsertRecords();
		const updateRecords = $table.getUpdateRecords()
		//console.log(updateRecords);
		if (insertRecords.length === 0 && updateRecords.length === 0) {
			VxeUI.modal.alert(`没有修改任何数据！`);
			return;
		}
		Meteor.call("saveYimInventory", insertRecords, updateRecords, (err, res) => {
			if (res.code === 200) {
				VxeUI.modal.message({ content: res.message, status: 'success' })
			} else {
				VxeUI.modal.alert(res.message);
			}
			if (err) {
				console.error("调用错误:", err.message);
			}
		})
	}
};

//获取余额
const getBalance = () => {
	Meteor.call("getAccountBalance", (err, res) => {
		if (err) {
			console.error("调用错误:", err.message);
		} else {
			console.log(res.data);
		}
	});
};
const pushEvent = () => {
	const $table = tableRef.value
	if ($table) {
		const record = {
			name: `Name_${new Date().getTime()}`
		}
		const { row: newRow } = $table.insert(record)
		$table.setEditCell(newRow, 'name')
	}
};

const removeRow = (row) => {
	const $table = tableRef.value
	if ($table) {
		$table.remove(row)
		VxeUI.modal.message({
			content: '数据已删除',
			status: 'success'
		})
	}
};
</script>
